﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using Maticsoft.DBUtility;

public partial class biz_coupon : FBasePage
{
    // Fields

    public string strCouponname = "";


    // Methods
    private void initPage()
    {
        if ((base.GetCookie("partnerid") == null) || (base.GetCookie("partnerid") == ""))
        {
            base.Response.Redirect("login.aspx");
        }
        string str = base.GetCookie("partnerid").ToString();
        string wherestr = " Partner_id=" + str;
        if ((this.txttitle.Text != null) && (this.txttitle.Text != ""))
        {
            wherestr = wherestr + " and Title like '%" + this.txttitle.Text + "%'";
        }
        if ((this.txtcoupon.Text != null) && (this.txtcoupon.Text != ""))
        {
            wherestr = wherestr + " and ID=" + this.txtcoupon.Text;
        }
        if (this.ddlstate.SelectedValue != "0")
        {
            wherestr = wherestr + " and  Consume='" + this.ddlstate.SelectedValue + "'";
        }
        StringBuilder builder = new StringBuilder();
        Pagers pagers = DbHelperSQL.SelectByPager("*", wherestr, "Create_time desc", this.pager.CurrentPageIndex, 10, "(SELECT   [User].Username, [User].Email, Team.Title,team.city_id, Coupon.* FROM         Coupon LEFT OUTER JOIN Team ON Coupon.Team_id = Team.Id LEFT OUTER JOIN [User] ON Coupon.User_id = [User].Id)t", string.Empty, string.Empty);
        builder.Append("<tr><th width=\"100\" nowrap>编号</th><th width=\"450\">项目名称</th><th width=\"160\">购买者</th><th width=\"60\" nowrap>密码</th><th width=\"100\" nowrap>开始时间</th><th width=\"100\" nowrap>有效日期</th><th width=\"160\">状态</th></tr>");
        DataTable myData = pagers.MyData;
        if (myData.Rows.Count > 0)
        {
            for (int i = 0; i < myData.Rows.Count; i++)
            {
                DataRow row = myData.Rows[i];
                if ((i % 2) != 0)
                {
                    builder.Append("<tr  id='team-list-id-" + row["id"] + "'>");
                }
                else
                {
                    builder.Append("<tr class=\"alt\"  id='team-list-id-" + row["id"] + "'>");
                }
                builder.Append("<td>" + row["id"].ToString() + "</td>");
                Maticsoft.Model.Category model = new Maticsoft.BLL.Category().GetModel(int.Parse(row["City_id"].ToString()));
                Maticsoft.Model.Team team2 = new Maticsoft.BLL.Team().GetModel(int.Parse(row["Team_id"].ToString()));
                builder.Append("<td>" + row["Team_id"].ToString() + "&nbsp;(<a class=\"deal-title\" href=\"/team.aspx?id=" + row["Team_id"].ToString() + "\" target=\"_blank\">" + team2.Title + "</a>)</td>");
                Maticsoft.Model.UserInfo info2 = new Maticsoft.BLL.UserInfo().GetModel(int.Parse(row["User_id"].ToString()));
                builder.Append("<td nowrap>" + info2.Email + "<br/>" + info2.Username + "</td>");
                if (base.ASSystem != null)
                {
                    if (base.ASSystem.partnerdown == 1)
                    {
                        builder.Append("<td>" + row["Secret"].ToString() + "</td>");
                    }
                    else
                    {
                        builder.Append("<td>*******</td>");
                    }
                }
                builder.Append("<td nowrap>" + ((row["start_time"] is DateTime) ? DateTime.Parse(row["start_time"].ToString()).ToString("yyyy-MM-dd") : string.Empty) + "</td>");
                builder.Append("<td nowrap>" + DateTime.Parse(row["Expire_time"].ToString()).ToString("yyyy-MM-dd") + "</td>");
                if (row["Consume"].ToString() == "Y")
                {
                    builder.Append("<td nowrap>已消费<br/>");
                }
                else if ((row["consume_time"] != null) && (row["consume_time"].ToString() != ""))
                {
                    if (DateTime.Parse(row["consume_time"].ToString()) < DateTime.Parse(row["expire_time"].ToString()))
                    {
                        builder.Append("<td nowrap>已过期</td>");
                    }
                    else
                    {
                        builder.Append("<td nowrap>有效</td>");
                    }
                }
                else
                {
                    builder.Append("<td nowrap>有效</td>");
                }
                builder.Append("</tr>");
            }
        }
        else
        {
            builder.Append("<tr><td colspan=\"6\">暂无数据！</td></tr>");
        }
        this.ltCouponn.Text = builder.ToString();
        this.pager.RecordCount = pagers.TotalRecords;
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        if (!this.Page.IsPostBack)
        {
            if (base.ASSystem != null)
            {
                this.strCouponname = base.ASSystem.Couponname;
            }
            this.initPage();
        }
        if (base.Request["btnselect"] == "筛选")
        {
            this.initPage();
        }
    }

    protected void pager_PageChanged(object sender, EventArgs e)
    {
        this.initPage();
    }

}